﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmHangtruong : Form
    {
        public frmHangtruong()
        {
            InitializeComponent();
        }
        cls_DM_Hangtruong objHangtruong = new cls_DM_Hangtruong();
        private void frmHangtruong_Load(object sender, EventArgs e)
        {
            GetAll_Hangtruong();
        }
        private void GetAll_Hangtruong()
        {
            gridControl1.DataSource = objHangtruong.GetAll_Hangtruong();
        }
        private void Save_Hangtruong()
        {
            clsDM_Hangtruong objdmht = new clsDM_Hangtruong();
            objdmht.TenHangTruong = gridView1.GetFocusedRowCellValue("TenHangTruong").ToString();
            objdmht.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmht.HangTruongID = 1;
                objdmht.isChangeable = false;
                blnResult = objHangtruong.Insert(objdmht);
            }
            else
            {
                objdmht.HangTruongID = (int)gridView1.GetFocusedRowCellValue("HangTruongID");
                objdmht.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objHangtruong.update(objdmht);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Hangtruong();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenHangTruong").ToString()) == DialogResult.No)
                return;
            objHangtruong.delete((int)gridView1.GetFocusedRowCellValue("HangTruongID"));
            GetAll_Hangtruong();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Hangtruong();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }

    }
}
